/**
 * 
 */
package com.yidu.finance.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.yidu.finance.model.FinanceType;
import com.yidu.finance.utils.JDBCTools;

/**
 * 描述：理财类型dao
 * @author zhouyuxian
 * @date 2019年9月23日 下午7:12:53
 * @ version 1.0
 */
public class FinanceTypeDao {
	
	/**
	 * 添加理财类型
	 * @param type
	 * @return
	 */
	public int insert(FinanceType type){
		int rows = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = JDBCTools.getCon();
			String sql = "insert into finance_type values(null,?,?,?)";
			pstm = con.prepareStatement(sql);
			pstm.setString(1, type.getTypeName());
			pstm.setString(2, type.getTypeRemarks());
			pstm.setInt(3, type.getTypeFlag());
			rows = pstm.executeUpdate();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pstm);
		}
		return rows;
	}
	
	/**
	 * 按ID删除理财类型
	 * @param typeID
	 * @return
	 */
	public int delete(int typeID){
		int rows = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = JDBCTools.getCon();
			String sql = "delete from finance_type where type_id=?";
			pstm = con.prepareStatement(sql);
			pstm.setInt(1, typeID);
			rows = pstm.executeUpdate();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pstm);
		}
		return rows;
	}
	
	/**
	 * 修改理财类型
	 * @param type
	 * @return
	 */
	public int update(FinanceType type){
		int rows = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = JDBCTools.getCon();
			String sql = "update finance_type set type_name=?,"
					+ "type_remarks=? ,type_flag=? where type_id=?" ;
			pstm = con.prepareStatement(sql);
			pstm.setString(1, type.getTypeName());
			pstm.setString(2, type.getTypeRemarks());
			pstm.setInt(3, type.getTypeFlag());
			pstm.setInt(4, type.getTypeID());
			rows = pstm.executeUpdate();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pstm);
		}
		return rows;
	}
	
	/**
	 * 查询全部理财类型
	 * @return
	 */
	public List<FinanceType> selectAll(){
		List<FinanceType> list = new ArrayList<FinanceType>();
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = JDBCTools.getCon();
			String sql = "select * from finance_type order by type_flag";
			pstm = con.prepareStatement(sql);
			rs = pstm.executeQuery();
			while(rs.next()){
				FinanceType type = new FinanceType();
				type.setTypeID(rs.getInt("type_id"));
				type.setTypeName(rs.getString("type_name"));
				type.setTypeRemarks(rs.getString("type_remarks"));
				type.setTypeFlag(rs.getInt("type_flag"));
				list.add(type);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pstm, rs);
		}
		return list;
	}
	
	/**
	 * 按ID查询理财类型
	 * @param financeID
	 * @return
	 */
	public FinanceType selectID(int typeID){
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = JDBCTools.getCon();
			String sql = "select * from finance_type where type_id=?";
			pstm = con.prepareStatement(sql);
			pstm.setInt(1, typeID);
			rs = pstm.executeQuery();
			while(rs.next()){
				FinanceType type = new FinanceType();
				type.setTypeID(rs.getInt("type_id"));
				type.setTypeName(rs.getString("type_name"));
				type.setTypeRemarks(rs.getString("type_remarks"));
				type.setTypeFlag(rs.getInt("type_flag"));
				return type;
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCTools.close(con, pstm, rs);
		}
		return null;
	}
}
